package com.zat.mes.andrmes.dofun.work


import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.data.BoxBean
import com.zat.mes.andrmes.dofun.QrScanActivity
import com.zat.mes.andrmes.dofun.work.vm.PackBoxModel
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.SystemActivityUtil
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.activity_packing.*
import kotlinx.android.synthetic.main.layout_title.*
import org.json.JSONObject
import permissions.dispatcher.*

/**
 * 装箱
 */
@RuntimePermissions
class PackingActivity : BaseActivity() {

    private var packBoxModel: PackBoxModel? = null
    private var boxBean: BoxBean? = null
    override fun init() {
        initView()
        initData()
    }

    private fun initData() {
        packBoxModel = ViewModelProvider(this).get(PackBoxModel::class.java)
        packBoxModel?.getBoxData()?.observe(this, Observer {
            closeLoading()
            boxBean = it
            tvPackNo.text = "箱号：" + it.boxNumber
        })
        packBoxModel?.getPostBox()?.observe(this, Observer {
            closeLoading()
            if (it.result != null) {
                tvPackNum.text = it.result
            } else {
                tvPackNum.text = ""
            }
            showToast(it.message)
        })

    }

    private fun initView() {
        setContentView(R.layout.activity_packing)
        tVtitle.text = "装箱"
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }

        btnPack.setOnClickListener { showCameraWithPermissionCheck() }
        btnSku.setOnClickListener { showCameraWithPermissionCheck() }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val qrCode = data?.getStringExtra(CompanionType.INTENT_EXTRA_QRCODE)
        if (resultCode == Activity.RESULT_OK) {
            //根据二维码箱子 或者 sku码

            if (qrCode != null && qrCode?.contains("qr:box:")) {
                //箱码
                showLoading()
                packBoxModel?.loadGetBox(qrCode)
            } else {
                val json = JSONObject()
                json.put("boxQRCode", boxBean?.boxQRCode)
                json.put("skuCode", qrCode)
                packBoxModel?.loadSkuBox(json.toString())
                showLoading()
            }
        }
    }

    @NeedsPermission(Manifest.permission.CAMERA)
    fun showCamera() {
        try {
            startActivityForResult(Intent(this, QrScanActivity::class.java), 0)
        } catch (e: Exception) {
            Log.e("activity", e.toString())
        }
    }

    @OnShowRationale(Manifest.permission.CAMERA)
    fun showRationaleForCamera(request: PermissionRequest) {
        request.proceed();
        Log.e("permission", "OnShowRationale")
    }

    @OnPermissionDenied(Manifest.permission.CAMERA)
    fun onCameraDenied() {
        showCameraWithPermissionCheck()
    }

    @OnNeverAskAgain(Manifest.permission.CAMERA)
    fun onCameraNeverAskAgain() {
//        showToast("请到设置打开相机权限")
        SystemActivityUtil.getAppDetailSettingIntent(this)
    }

    @SuppressLint("NeedOnRequestPermissionsResult")
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        onRequestPermissionsResult(requestCode, grantResults)

    }

}